package com.my.mymafengwo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.ParcelFileDescriptor;

import java.io.FileDescriptor;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/5 12:50
 * copyright:
 * name:
 * step:
 * ***********************************************
 */
public class BitmapUtils {

    /**
     * 获得圆角图片
     *
     * @param bit
     * @return
     */
    public static Bitmap getRoundBitmap(Bitmap bit) {
        // Bitmap bit = getSampleBitmap(b, 200);
        Bitmap bitmap = Bitmap.createBitmap(bit.getWidth(), bit.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        int width = bit.getWidth();
        int height = bit.getHeight();
        int len = width > height ? height : width;
        // float dx = width / len;
        // float dy = height / len;
        // Matrix matrix = new Matrix();
        // matrix.postScale(dx, dy);
        // RectF rectF = new RectF(0, 0, len, len);
        // canvas.drawRoundRect(rectF, 90, 90, paint);
        canvas.drawCircle(width / 2, height / 2, len / 2 - (len / 30), paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bit, 0, 0, paint);
        // canvas.drawBitmap(bit, matrix, paint);
        return bitmap;
    }

    /**
     * @param path 要转换的图片路径
     * @param size 要转换到的目标大小
     * @return
     */
    public static Bitmap getSampleBitmap(String path, int size) {
        if (path == null) {
            return null;
        }
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeFile(path, opts);
        int width = opts.outWidth;
        int height = opts.outHeight;
        int dx = width / size;
        int dy = height / size;
        int d = dx > dy ? dx : dy;
        opts.inSampleSize = d;
        opts.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, opts);
    }

    /**
     * @param pfd  文件路径
     * @param size 要转换到的目标大小
     * @return
     */
    public static Bitmap getSampleBitmap(ParcelFileDescriptor pfd, int size) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        FileDescriptor fd = null;
        if (pfd != null) {
            fd = pfd.getFileDescriptor();
        }
        Bitmap bm = BitmapFactory.decodeFileDescriptor(fd, null, opts);
        int width = opts.outWidth;
        int height = opts.outHeight;
        int dx = width / size;
        int dy = height / size;
        int d = dx > dy ? dx : dy;
        if (d > 1) {
            opts.inSampleSize = d;
        } else {
            opts.inSampleSize = 1;
        }
        opts.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fd, null, opts);
    }

}
